polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Other Topics / Internal-Snooping.html
blob628e8382b12e272994c9f7226104dbcae446f9f2
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.43">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0022b8}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #9f261f}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #ae1a19}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
22 span.s1 {color: #000000}
23 span.s2 {color: #0022b8}
24 span.s3 {color: #606060}
25 span.s4 {color: #3b6c0f}
26 span.s5 {color: #9f261f}
27 span.s6 {color: #0016bd}
28 span.Apple-tab-span {white-space:pre}
29 </style>
30 </head>
31 <body>
32 <p class="p1"><b>Snooping around SuperCollider</b></p>
33 <p class="p2"><br></p>
34 <p class="p3">You can inspect much of the internal structure of the class library and other data structures.</p>
35 <p class="p3">This can often be useful for research and debugging purposes.</p>
36 <p class="p2"><br></p>
37 <p class="p4"><b>Class Definitions, Implementations, and References</b></p>
38 <p class="p2"><br></p>
39 <p class="p3">Selecting the name of any Class (e.g. <b>Object</b>) and typing cmd-j will open its class definition file.</p>
40 <p class="p2"><br></p>
41 <p class="p3"><b>** Note:</b> Any keypresses that open files identify the files using SuperCollider strings, which may contain only ASCII characters up to code point 127. If any part of the path to your SuperCollider installation contains character codes greater than 127 (such as the old Mac convention of identifying program folders with option-F), automatic file opening will fail.</p>
42 <p class="p2"><br></p>
43 <p class="p3">Selecting the name of any method (e.g. <b>play</b>) and typing cmd-y will open a window showing all implementations of that method and their arguments. Selecting one of those classes and methods (e.g. <b>Sample:play</b>) and typing cmd-j will open the class definition at that method. (Note that cmd-y only shows implementations, and does not indicate inheritance).</p>
44 <p class="p2"><br></p>
45 <p class="p3">Selecting any text (e.g. <b>SCWindow </b>or <b>asStream</b>) and typing shift-cmd-y will open a window showing all references to the selected text, i.e. each place it is used within the class library. (This will not find methods calls compiled with special byte codes like 'value'.)</p>
46 <p class="p2"><br></p>
47 <p class="p3">SC has a graphical Class browser which will show all methods, arguments, subclasses, instance variables and class variables. Using the browser's buttons you can easily navigate to the class' superclass, subclasses, class source, method source, helpfile (if there is one), check references or implementation of methods, or even open a web browser to view the corresponding entry in the online SVN repository.</p>
48 <p class="p2"><br></p>
49 <p class="p5">SequenceableCollection<span class="s1">.browse;</span></p>
50 <p class="p2"><br></p>
51 <p class="p4"><b>Snooping in Classes</b></p>
52 <p class="p2"><br></p>
53 <p class="p3">The <b>Class</b> help file documents some of these snooping methods.</p>
54 <p class="p3">Even though you may access these data structures, if you store things into them, you may break something.</p>
55 <p class="p6"><br></p>
56 <p class="p7"><span class="s2">Collection</span><span class="s1">.dumpInterface;<span class="Apple-tab-span"> </span></span>// print all instance methods defined for this class</p>
57 <p class="p8"><br></p>
58 <p class="p7"><span class="s2">Collection</span><span class="s1">.class.dumpInterface;<span class="Apple-tab-span"> </span></span>// print all class methods defined for this class</p>
59 <p class="p8"><br></p>
60 <p class="p8"><br></p>
61 <p class="p7">// The following three include inherited methods</p>
62 <p class="p8"><br></p>
63 <p class="p9"><span class="s2">Collection</span>.methods.collect(<span class="s2">_</span>.name);<span class="Apple-tab-span"> </span></p>
64 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// print all instance methods that instances of this class respond to</p>
65 <p class="p8"><br></p>
66 <p class="p9"><span class="s2">Collection</span>.class.methods.collect(<span class="s2">_</span>.name);<span class="Apple-converted-space"> </span></p>
67 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// print all class methods that this class responds to</p>
68 <p class="p8"><br></p>
69 <p class="p7"><span class="s2">Collection</span><span class="s1">.dumpFullInterface;<span class="Apple-tab-span"> </span></span>// print all instance and class methods that this class responds to</p>
70 <p class="p8"><br></p>
71 <p class="p7"><span class="s2">Collection</span><span class="s1">.dumpMethodList;<span class="Apple-tab-span"> </span></span>// print instance methods of this class and superclasses, in alpha order</p>
72 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// also shows from which class the method is inherited</p>
73 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// does not include Object or Class methods</p>
74 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// for class methods, do Meta_Collection.dumpMethodList</p>
75 <p class="p8"><br></p>
76 <p class="p7"><span class="s2">Collection</span><span class="s1">.dumpClassSubtree;<span class="Apple-tab-span"> </span></span>// dump all subclasses of this class</p>
77 <p class="p8"><br></p>
78 <p class="p7"><span class="s2">Collection</span><span class="s1">.dumpSubclassList;<span class="Apple-tab-span"> </span></span>// dump all subclasses, in alphabetical order</p>
79 <p class="p8"><br></p>
80 <p class="p7"><span class="s2">SCWindow</span><span class="s1">.instVarNames.dump;<span class="Apple-tab-span"> </span></span>// dump all instance variable names of this class</p>
81 <p class="p8"><br></p>
82 <p class="p7"><span class="s2">SCWindow</span><span class="s1">.classVarNames.dump;<span class="Apple-tab-span"> </span></span>// dump all class variable names of this class</p>
83 <p class="p8"><br></p>
84 <p class="p7"><span class="s2">SCWindow</span><span class="s1">.filenameSymbol.postln;<span class="Apple-tab-span"> </span></span>// the path to the file that defined this class</p>
85 <p class="p8"><br></p>
86 <p class="p9">(</p>
87 <p class="p7">// print all classes whose names start with 'F'</p>
88 <p class="p9"><span class="s2">Class</span>.allClasses.do({ <span class="s2">arg</span> class;</p>
89 <p class="p9"><span class="Apple-tab-span"> </span>if (class.name.asString.at(0) == $F, { class.name.postln; });</p>
90 <p class="p9">})</p>
91 <p class="p9">)</p>
92 <p class="p8"><br></p>
93 <p class="p9">(</p>
94 <p class="p7">// find and print all class variable names defined in the system</p>
95 <p class="p9"><span class="s2">Class</span>.allClasses.do({ <span class="s2">arg</span> class;</p>
96 <p class="p9"><span class="Apple-converted-space">    </span>if (class.classVarNames.notNil, {</p>
97 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// classVarNames is an Array of Symbols</p>
98 <p class="p9"><span class="Apple-converted-space">        </span>class.classVarNames.do({ <span class="s2">arg</span> varname;</p>
99 <p class="p9"><span class="Apple-converted-space">            </span>(class.name.asString ++ <span class="s3">" "</span> ++ varname.asString).postln;</p>
100 <p class="p9"><span class="Apple-converted-space">        </span>})</p>
101 <p class="p9"><span class="Apple-converted-space">    </span>});</p>
102 <p class="p9">});</p>
103 <p class="p9">)</p>
104 <p class="p8"><br></p>
105 <p class="p9">(</p>
106 <p class="p7">// find and print all methods that contain "ascii"</p>
107 <p class="p9"><span class="s2">Class</span>.allClasses.do({ <span class="s2">arg</span> class;</p>
108 <p class="p9"><span class="Apple-converted-space">   <span class="Apple-tab-span"> </span></span>class.methods.do({ <span class="s2">arg</span> sel;</p>
109 <p class="p9"><span class="Apple-converted-space">   <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>if(sel.name.asString.find(<span class="s3">"ascii"</span>).notNil) {</p>
110 <p class="p9"><span class="Apple-converted-space">            <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>(class.name.asString + <span class="s3">"-"</span> + sel.name).postln;</p>
111 <p class="p9"><span class="Apple-converted-space">            </span>}</p>
112 <p class="p9"><span class="Apple-converted-space">      </span>});</p>
113 <p class="p9">}); <span class="s3">""</span></p>
114 <p class="p9">)</p>
115 <p class="p6"><br></p>
116 <p class="p4"><b>Snooping in Methods</b></p>
117 <p class="p6"><br></p>
118 <p class="p3">Same thing goes here, if you store things into Methods, you may break something.</p>
119 <p class="p6"><br></p>
120 <p class="p7"><span class="s2">Collection</span><span class="s1">.findMethod(</span><span class="s4">'select'</span><span class="s1">); </span>// does it have this method?</p>
121 <p class="p8"><br></p>
122 <p class="p7"><span class="s2">Array</span><span class="s1">.findMethod(</span><span class="s4">'select'</span><span class="s1">); </span>// this class doesn't</p>
123 <p class="p6"><br></p>
124 <p class="p7"><span class="s2">Array</span><span class="s1">.findRespondingMethodFor(</span><span class="s4">'select'</span><span class="s1">); </span>// climb the class tree to find the method</p>
125 <p class="p8"><br></p>
126 <p class="p7"><span class="s2">Collection</span><span class="s1">.findMethod(</span><span class="s4">'select'</span><span class="s1">).dump; </span>// find a method object</p>
127 <p class="p8"><br></p>
128 <p class="p9"><span class="s2">Collection</span>.findMethod(<span class="s4">'select'</span>).argNames.dump; <span class="s5">// dump its argument names</span></p>
129 <p class="p8"><br></p>
130 <p class="p7"><span class="s2">Collection</span><span class="s1">.findMethod(</span><span class="s4">'select'</span><span class="s1">).varNames.dump; </span>// dump its local variable names</p>
131 <p class="p8"><br></p>
132 <p class="p7">// dump its code. mostly for debugging the compiler.</p>
133 <p class="p9"><span class="s2">Collection</span>.findMethod(<span class="s4">'select'</span>).dumpByteCodes;<span class="Apple-tab-span"> </span></p>
134 <p class="p8"><br></p>
135 <p class="p7"><span class="s2">Collection</span><span class="s1">.dumpByteCodes(</span><span class="s4">'select'</span><span class="s1">);<span class="Apple-tab-span"> </span></span>// a shorter version of the above</p>
136 <p class="p8"><br></p>
137 <p class="p7"><span class="s1">{ 1 + 2 }.dump; </span>// this is a Function</p>
138 <p class="p8"><br></p>
139 <p class="p7"><span class="s1">{ 1 + 2 }.def.dump;<span class="Apple-tab-span"> </span></span>// get its FunctionDef</p>
140 <p class="p8"><br></p>
141 <p class="p9">{ 1 + 2 }.def.dumpByteCodes;<span class="Apple-tab-span"> </span><span class="s5">// dump its code.</span></p>
142 <p class="p8"><br></p>
143 <p class="p6"><br></p>
144 <p class="p4"><b>Snooping in Windows</b></p>
145 <p class="p10"><br></p>
146 <p class="p9">(</p>
147 <p class="p11">// create some windows to snoop in</p>
148 <p class="p9">5.do({ <span class="s6">arg</span> i;</p>
149 <p class="p9"><span class="Apple-tab-span"> </span><span class="s6">var</span> w, b;</p>
150 <p class="p9"><span class="Apple-tab-span"> </span>w = <span class="s6">Window</span>.new(<span class="s3">"snoop "</span> ++ i.asString,<span class="Apple-converted-space"> </span></p>
151 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s6">Rect</span>.new( 200 + 400.rand, 69 + 300.rand, 172, 90 ));</p>
152 <p class="p9"><span class="Apple-tab-span"> </span>w.front;</p>
153 <p class="p9"><span class="Apple-tab-span"> </span>b = <span class="s6">Button</span>.new( w, <span class="s6">Rect</span>.new( 23, 28, 127, 25 ));</p>
154 <p class="p9"><span class="Apple-tab-span"> </span>b.states = [[<span class="s3">"BLAM-O"</span>, <span class="s6">Color</span>.red]];</p>
155 <p class="p9">}))</p>
156 <p class="p8"><br></p>
157 <p class="p7"><span class="s6">Window</span><span class="s1">.allWindows.dump;<span class="Apple-tab-span"> </span></span>// dump a list of all open SCWindows</p>
158 <p class="p8"><br></p>
159 <p class="p7">// a little more helpful, dump their names</p>
160 <p class="p9"><span class="s6">Window</span>.allWindows.collect({ <span class="s2">arg</span> w; w.name }).postln;<span class="Apple-tab-span"> </span></p>
161 <p class="p8"><br></p>
162 <p class="p9">(</p>
163 <p class="p7">// change background colors of all open windows</p>
164 <p class="p9"><span class="s6">Window</span>.allWindows.do({ <span class="s2">arg</span> window;</p>
165 <p class="p9"><span class="Apple-tab-span"> </span>window.view.background = <span class="s2">Color</span>.new(0.5 + 0.5.rand, 0.5 + 0.5.rand, 0.5 + 0.5.rand);</p>
166 <p class="p9">}))</p>
167 <p class="p8"><br></p>
168 <p class="p7"><span class="s6">Window</span><span class="s1">.closeAll; </span>// close all the windows (This will close the server windows)</p>
169 <p class="p8"><br></p>
170 <p class="p4"><b>Snooping in SynthDefs</b></p>
171 <p class="p6"><br></p>
172 <p class="p7">// First execute this:</p>
173 <p class="p9">(</p>
174 <p class="p12"><span class="s1">f = </span><span class="s2">SynthDef</span><span class="s1">(</span>"Help-SnoopSynthDef"<span class="s1">,<span class="Apple-converted-space"> </span></span></p>
175 <p class="p9"><span class="Apple-tab-span"> </span>{ <span class="s2">arg</span> out=0;</p>
176 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">Out</span>.ar(out, <span class="s2">PinkNoise</span>.ar(0.1))</p>
177 <p class="p9"><span class="Apple-tab-span"> </span>});</p>
178 <p class="p9">)</p>
179 <p class="p8"><br></p>
180 <p class="p7"><span class="s1">f.dumpUGens;<span class="Apple-tab-span"> </span></span>// get the ugens, listed in order of execution, with rate, index and</p>
181 <p class="p7"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// inputs</p>
182 <p class="p8"><br></p>
183 <p class="p4"><b>Snooping in the Interpreter</b></p>
184 <p class="p2"><br></p>
185 <p class="p3">When evaluating text in the interpreter, the variable 'this' always refers to the interpreter.</p>
186 <p class="p6"><br></p>
187 <p class="p7"><span class="s2">this</span><span class="s1">.dump;<span class="Apple-tab-span"> </span></span>// display the values of all the interpreter variables a-z</p>
188 <p class="p8"><br></p>
189 <p class="p7"><span class="s2">this</span><span class="s1">.clearAll;<span class="Apple-tab-span"> </span></span>// set all variables a-z to nil</p>
190 <p class="p8"><br></p>
191 <p class="p7"><span class="s1">g = </span><span class="s2">this</span><span class="s1">.compile(</span><span class="s3">"(1 + 2).postln"</span><span class="s1">);<span class="Apple-tab-span"> </span></span>// compile some text into a Function</p>
192 <p class="p8"><br></p>
193 <p class="p7"><span class="s1">g.postln;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// see, g is a Function</p>
194 <p class="p8"><br></p>
195 <p class="p7"><span class="s1">g.value;<span class="Apple-tab-span"> </span></span>// evaluate g</p>
196 <p class="p8"><br></p>
197 <p class="p7"><span class="s2">this</span><span class="s1">.interpret(</span><span class="s3">"(1 + 2).postln"</span><span class="s1">);<span class="Apple-tab-span"> </span></span>// interpret some text</p>
198 <p class="p8"><br></p>
199 <p class="p7"><span class="s2">this</span><span class="s1">.interpretPrint(</span><span class="s3">"1 + 2"</span><span class="s1">);<span class="Apple-tab-span"> </span></span>// interpret some text and print the result</p>
200 <p class="p8"><br></p>
201 <p class="p8"><br></p>
202 <p class="p6"><br></p>
203 </body>
204 </html>